package com.itheima.service.system.impl;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.UserDao;
import com.itheima.domain.system.User;
import com.itheima.service.system.UserService;
import com.itheima.utils.AliyunSmsUtils;
import com.itheima.utils.DaoInstanceFactory;
import com.itheima.utils.JedisUtils;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import static com.itheima.utils.AliyunSmsUtils.sendSms;

/**
 * @Author: Mr.yuan
 * @Description: 通过动态代理获得数据
 * @Date: Created in 22:13 2020/11/29
 * @ModifiedBy:
 */

public class UserServiceImpl implements UserService {
    /**
     * @Description: 代理登录
     * @Param: [user]
     * @return: com.itheima.domain.system.User
     * @Author: Mr.yuan
     * @Date: 2020/11/30
     */
    @Override
    public User login(User user) {
        UserDao mapper = DaoInstanceFactory.getMapper(UserDao.class);
        return mapper.findByUNameAndPWd(user);
    }


    /**
     * @Description: 通过id进行查询用户
     * @Param: [id]
     * @return: com.itheima.domain.system.User
     * @Author: Mr.yuan
     * @Date: 2020/11/30
     */
    @Override
    public User findById(String id) {
        UserDao mapper = DaoInstanceFactory.getMapper(UserDao.class);

        return mapper.findById(id);
    }

    /**
     * @Description: 更改用户密码
     * @Param: [user]
     * @return: void
     * @Author: Mr.yuan
     * @Date: 2020/11/30
     */
    @Override
    public void updatePWD(User user) {
        UserDao mapper = DaoInstanceFactory.getMapper(UserDao.class);
        mapper.updatePWD(user);


    }

    /**
     * @Description: 根据用户名称模糊查询所有
     * @Param:[username, pageNum, pageSize]
     * @return:com.github.pagehelper.PageInfo<com.itheima.domain.system.User>
     * @Author:Mr.Cheng
     * @Date:2020/11/30
     */
    @Override
    public PageInfo<User> findAllByUserNameLike(String username, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = DaoInstanceFactory.getMapper(UserDao.class).findAllByUserNameLike(username);
        PageInfo<User> list = new PageInfo<>(userList);
        return list;
    }

    /**
     * @Description: 新增用户
     * @Param:[user]
     * @return:void
     * @Author:Mr.Cheng
     * @Date:2020/11/30
     */
    @Override
    public void save(User user) {
        user.setId(UUID.randomUUID().toString().replace("-", "").toUpperCase());
        DaoInstanceFactory.getMapper(UserDao.class).save(user);
    }

    /**
     * @Description: 批量删除用户
     * @Param:[userId]
     * @return:void
     * @Author:Mr.Cheng
     * @Date:2020/11/30
     */
    @Override
    public void delete(String userId) {
        DaoInstanceFactory.getMapper(UserDao.class).deleteUsersRoleByUserId(userId);
        DaoInstanceFactory.getMapper(UserDao.class).delete(userId);
    }

    /**
     * @Description: 根据id查找用户
     * @Param:[userId]
     * @return:com.itheima.domain.system.User
     * @Author:Mr.Cheng
     * @Date:2020/11/30
     */
    @Override
    public User findByUserId(String userId) {
        User user = new User();
        try {
            user = DaoInstanceFactory.getMapper(UserDao.class).findByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    /**
     * @Description: 根据id查找用户属性
     * @Param:[userId]
     * @return:java.util.List<java.util.Map>
     * @Author:Mr.Cheng
     * @Date:2020/11/30
     */
    @Override
    public List<Map> findAllRoleByUserId(String userId) {
        return DaoInstanceFactory.getMapper(UserDao.class).findAllRoleByUserId(userId);
    }

    /**
     * @Description: 将配置好的用户属性进行保存
     * @Param:[roleIds, userId]
     * @return:void
     * @Author:Mr.Cheng
     * @Date:2020/11/30
     */
    @Override
    public void saveRoleToUser(String[] roleIds, String userId) {
        DaoInstanceFactory.getMapper(UserDao.class).deleteUsersRoleByUserId(userId);
        if (roleIds != null) {
            for (String roleId : roleIds) {
                com.itheima.factory.DaoInstanceFactory.getMapper(UserDao.class).saveRoleToUser(roleId, userId);
            }
        }
    }

    /**
     * @Description: 修改用户
     * @Param:[users]
     * @return:void
     * @Author:Mr.Cheng
     * @Date:2020/11/30
     */
    @Override
    public void update(User users) {
        UserDao mapper = DaoInstanceFactory.getMapper(UserDao.class);
        mapper.update(users);
    }

    /**
     * @Description: 獲取手機驗證碼
     * @Param: [phoneNum]
     * @return: java.lang.String
     * @Author: Mr.yuan
     * @Date: 2020/12/1
     */

    @Override
    public String findPW(String phoneNum) {
        Jedis jedis = JedisUtils.getJedis();
        return jedis.get("code");


    }

    /**
     * @Description: 設置手機驗證碼進入redis並在十分鐘後銷毀
     * @Param: [phoneNum]
     * @return: void
     * @Author: Mr.yuan
     * @Date: 2020/12/1
     */
    @Override
    public void setPhoneCode(String phoneNum) {
        AliyunSmsUtils.setNewcode();
        String code = Integer.toString(AliyunSmsUtils.getNewcode());
        Jedis jedis = JedisUtils.getJedis();
        jedis.set("code", code);
        jedis.expire("code",600);

        try {
            SendSmsResponse response = sendSms(phoneNum, code);

        } catch (ClientException e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }

    /**
     * @Description: 通過手機號查詢用戶
     * @Param: [phoneNum]
     * @return: com.itheima.domain.system.User
     * @Author: Mr.yuan
     * @Date: 2020/12/1
     */

    @Override
    public User findByPhone(String phoneNum) {

        UserDao mapper = DaoInstanceFactory.getMapper(UserDao.class);
        return mapper.findByPhoneNum(phoneNum);
    }

     /**
          * @Description: 根据用户id查询该用户具有的所有权限信息
          * @Param:
          * @return:
          * @Author: gjp
          * @Date:    2020/12/01
          */
    @Override
    public List<String> findAllPermissionUrlByUserId(String userId) {
        return DaoInstanceFactory.getMapper(UserDao.class).findAllPermissionUrlByUserId(userId);
    }

}
